package com.usian.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbItemCatMapper;
import com.usian.mapper.TbItemMapper;
import com.usian.mapper.TbItemParamItemMapper;
import com.usian.mapper.TbItemParamMapper;
import com.usian.pojo.*;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ParamServiceImpl implements ParamService {
    @Autowired
    private TbItemParamMapper itemParamMapper;

    @Override
    public TbItemParam selectItemParamByItemCatId(Integer itemCatId) {
        TbItemParamExample tbItemParamExample=new TbItemParamExample();
        TbItemParamExample.Criteria criteria = tbItemParamExample.createCriteria();
        criteria.andItemCatIdEqualTo((long)itemCatId);

        List<TbItemParam> list = itemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);
        if (list.size()>0){
            return list.get(0);
        }

        return null;
    }

    @Override
    public PageResult selectItemParamAll() {
        PageHelper.startPage(1,5);
        List<TbItemParam> list = itemParamMapper.selectByExampleWithBLOBs(null);
        PageInfo<TbItemParam>page=new PageInfo<>(list);

        return new PageResult(page.getPageNum(),page.getPages(),page.getList());
    }

    @Override
    public int insertItemParam(Integer itemCatId, String paramData) {
        Date date=new Date();
        TbItemParam tbItemParam=new TbItemParam();
        tbItemParam.setParamData(paramData);
        tbItemParam.setItemCatId((long)itemCatId);
        tbItemParam.setCreated(date);
        tbItemParam.setUpdated(date);

        return itemParamMapper.insertSelective(tbItemParam);
    }

    @Override
    public int deleteItemParamById(Integer id) {

        return itemParamMapper.deleteByPrimaryKey((long)id);
    }
}
